/*
 * full-length, trailer home index 
 */
$(function() {
	movie_index.run();
});

var movie_index = {
	run : function() {
		this._init_();
		this._start_();
		this._init_events();
	},

	_init_ : function() {
		this.initial = $("#alpha").val() || "";
		this.genre = $("#genre").val() || "";
		this.decade = $("decade").val() || "";
		this.sort = "";
		this.itemUrl = "";
		this.title = "";
		this.scrollPage = 1;		
		this.category = $("#category").val();	
		this.pageCount = parseInt($("#pageCount").val()) || 0;
		this.queryUrl = main.contextPath + "/movie/" + this.category + "/query";
		this.vdInstalled = vd.check();
	},

	_start_ : function() {
		side_pub._init_side_genre_decade(this);
		
		var loc = location.href;		
		var index = loc.indexOf("#");
		if(index > 0){
			this.params = loc.substring(index + 1);
		}
		if(this.params){
			this._query_movie(1, this.params);
		}else{
			this._bind_image_events($("#movie_content_div"));
		}		
	
		var index = 3;
		if(this.category == 'full-length'){
			index = 1;
		}
		$("#movie_side_ul > li").eq(index).addClass("curr");
	},
	
	_bind_image_events: function($div){
		$("div.img", $div).mouseenter(function(){
			$(this).children('div.tc_btn').fadeIn("slow");
		}).mouseleave(function(){
			$(this).children('div.tc_btn').stop(true,false).fadeOut("fast");
		});
		
		$("div.mi_box a", $div).click(function(){
			var $this = $(this);
			if($this.text() == 'Download'){
				return;
			}
			
			var href = $this.attr("href");
			var params = "";
			if(movie_index.genre){
				params += "genre=" + movie_index.genre;
			}
			if(movie_index.decade){
				if(params){
					params += "&";
				}
				params += "decade=" + movie_index.decade;
			}
			
			if(params){
				href += "#" + params;
			}
			
			location.href = href;
			return false;
		});
	},

	_init_events : function() {
		this._bind_events();
		
		$.scroll(function(){
			movie_index._handle_scroll();
		});
	},	

	_bind_events : function() {
		$("#genre_div > div > ul > li > a").click(function() {
			var $this = $(this);			
			$("#genre_div").find(".selected").removeAttr("class");
			$this.addClass("selected");
			movie_index._reset_alpha_line();
			
			movie_index.genre = $this.attr("genre");
			movie_index.initial = "";
			movie_index.sort = "";			
			movie_index._rewrite_location_value();
			movie_index._query_movie();			
			return false;
		});

		$("#decade_div > ul > li > a").click(function() {
			var $this = $(this);			
			$("#decade_div > ul > li > a").removeAttr("class");
			$this.addClass("selected");			
			movie_index._reset_alpha_line();
			
			movie_index.decade = $this.attr("decade");;
			movie_index.initial = "";
			movie_index.sort = "";			
			movie_index._rewrite_location_value();
			movie_index._query_movie();
			return false;
		});
		
		$("#alpha_div > a.initial").click(function() {
			var $this = $(this);
			var temp = $this.text();
			$("#alpha_div > a.initial").removeClass("selected");
			$this.addClass("selected");
			if("Popular" == temp){
				movie_index.sort = "";
				movie_index.initial = null;
			}else if("Time" == temp){
				movie_index.sort = "time";
				movie_index.initial = null;
			}else{
				movie_index.initial = $this.text();
				//movie_index._rewrite_location_value();
				movie_index.sort = "popular";
			}							
			
			movie_index._query_movie();
			return false;
		});
	},
	
	_reset_alpha_line: function(){
		$("#alpha_div > a.initial").removeClass("selected");
		$("#alpha_div > a.popular").addClass("selected");
	},
	
	_handle_scroll: function(){	
		if(this._has_more_movies()){
			var $w = $(window);
			if( ! $w.data("scrolling")){
				$w.data("scrolling", true);
				this._query_movie(this.scrollPage + 1);
			}	
		}			
	},
	
	_has_more_movies: function(){
		return this.pageCount > 0 && this.scrollPage < this.pageCount;
	},

	_query_movie : function(page, params) {
		page = page || 1;		
		movie_index.scrollPage = page;					
		
		var data = null;
		if (params) {
			data = params;
		} else {
			data = this._gather_params();
		}
		
		var url = this.queryUrl + "?page=" + page;
		var $w = $(window);
		var $loading = $("#movie_content_div > div.loadingBox");		
		$.ajax(url, {
			data: data,
			beforeSend: function(){
				if(page == 1){
					$("#movie_content_div").html($loading).append("<div class=\"clear\"></div>");
				}
				$loading.show();
			},
			complete: function(){
				$loading.hide();
				$w.data("scrolling", false);						
			},
			success: function(result){		
				result = movie_index._bind_fly_result_events(result);
				$loading.before(result);	
				if(page == 1){				
					movie_index.pageCount = parseInt($("#pageCount").val()) || 0;
					
					//考虑大屏问题
					if($w.height() >= $(document).height() 
							&& movie_index._has_more_movies() ){
						movie_index._query_movie(2);
					}
				}
			}			
		});
	},
	
	_bind_fly_result_events: function(result){
		var $div = $("<div>" + result + "</div>");
		this._bind_image_events($div);
		return $div.children();
	},
	
	_rewrite_location_value : function() {
		var loc = location.href;
		var index = loc.indexOf("#");
		if(index > 0){
			loc = loc.substring(0, index);
		}
		
		var params = this._get_params();
		if(params){
			loc += "#" + params;
		}		
		
		location.href = loc;
	},
	
	_get_params: function(){
		var params = [];		
		if (this.genre) {
			params.push("genre=" + this.genre);
		}
		if (this.decade) {
			params.push("decade=" + this.decade);
		}
		
		if(params.length > 0){
			return params.join("&");
		}
	},
	
	_gather_params : function() {
		var params = [];
		if (this.initial) {
			params.push("initial=" + this.initial);
		}		
		if (this.sort) {
			params.push("sort=" + this.sort);
		}
		
		var p = this._get_params();
		if(p){
			params.push(p);
		}
		if(params.length > 0){
			return params.join("&");
		}		
	}
};